package com.utn.ab.demo.controller;
import com.utn.ab.common.util.FileUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建人 liuchunlin
 * @创建时间 13:34
 * @描述
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @RequestMapping("/currentUser")
    public Map currentUser(){
        Map rstMap = new HashMap();
        rstMap.put("from","8080");
        rstMap.put("hello","world");
        return rstMap;
    }

    @RequestMapping(value = "/post")
    @ResponseBody
    //public String testPost(@RequestBody String json){
    public String testPost(@RequestBody Map map){
        //public String testPost(@RequestBody Model model){
        //表单可以
        //public String testPost(Model model){
        //public String testPost(@ModelAttribute Model model){
        //public String testPost(String userId,String productId){
        //public String testPost(@RequestParam("userId") String userId,@RequestParam("productId") String productId){
        //public String testPost(HttpServletRequest request){
        //String userId = request.getParameter("userId");
        //String productId = request.getParameter("productId");
        //String devices = request.getParameter("devices");
        return "success";
    }

    @RequestMapping(value = "/{name}")
    @ResponseBody
    public Object file2Json(@PathVariable String name){
        try {
            return FileUtils.readToObject("json/"+ name +".json",HashMap.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
